package Client;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

import Client.Action.LoadRole;
import Client.Action.Login;
import Client.UI.MyUIManager;
import Common.Constant.CommonConstant;

public class Receiver extends Thread{
	private DataInputStream	dIn;
	private Client c;
	
	public Receiver(Client c, InputStream in){
		this.c = c;
		dIn = new DataInputStream(in);
		this.start();
	}
	
	public void run(){	
		try {
			while(true){ 
				byte bTransModel = dIn.readByte();
				byte bFunctionTp = dIn.readByte();
				byte[] bData = new byte[dIn.available()];
				dIn.read(bData);
				chooseModel(bTransModel, bFunctionTp, bData);
			}
		} catch (IOException ex) {
			if(ex.getMessage().equals(CommonConstant.CONNECTION_RESET)){
				MyUIManager.getInstance().getDisconnectDialog();
			} else {
				ex.printStackTrace();
			}
		}
	}
	
	private void chooseModel(byte bTransModel, byte bFunctionTp, byte[] bData){
		switch(bTransModel){
			case CommonConstant.TRANS_MODEL_ACCOUNT:
				Login.loginStatus(bFunctionTp);
				break;
			case CommonConstant.TRANS_MODEL_ROLE:
				LoadRole.rolerStatus(bFunctionTp, bData);
				break;
		}
	}
}
